﻿using System;
using System.Text.RegularExpressions;

namespace FirstHomeWork.Utility
{
    /// <summary>
    /// 字符串扩展方法
    /// </summary>
    public static class StringExtension
    {
        /// <summary> 
        /// 判断字符串是否为整数 
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsNumber(this string str)
        {
            try
            {
                var num = Int32.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary> 
        /// 判断字符串是否为数字
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsDemical(this string str)
        {
            try
            {
                var demical = Convert.ToDecimal(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 判断字符串是否为固定电话号码
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsTelephone(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string teleRegex = @"0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}";
            return Regex.IsMatch(str, teleRegex);
        }

        /// <summary>
        /// 判断字符串是否为手机号码
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsCellphone(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string cellRegex = @"^(13|14|15|16|17|18|19)[0-9]{9}$";
            return Regex.IsMatch(str, cellRegex);
        }

        /// <summary>
        /// 判断字符串是否为URL
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsURL(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string urlRegex = @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?";
            return Regex.IsMatch(str, urlRegex);
        }

        /// <summary>
        /// 判断字符串是否为Email
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsEmail(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string emailRegex = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}";
            return Regex.IsMatch(str, emailRegex, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 判断字符串是否包含汉字
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsExistChineseWord(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string chaWordRegex = @"[\u4e00-\u9fa5]";
            return Regex.IsMatch(str, chaWordRegex);
        }

        /// <summary>
        /// 判断字符串是否为邮政编码
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>如果字符串为有效的邮政编码，则为 true；否则为 false。</returns>
        public static bool IsPostCode(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string postCodeRegex = @"^\d{6}$";
            return Regex.IsMatch(str, postCodeRegex);
        }

        /// <summary>
        /// 是否是身份证号码
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是返回true，否返回false</returns>
        public static bool IsIdentity(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string idRegex15 = @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$";
            string idRegex18 = @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$";
            return Regex.IsMatch(str, idRegex15) || Regex.IsMatch(str, idRegex18);
        }

        /// <summary>
        /// 判断字符串是否为传真号码
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>如果字符串为有效的传真号码，则为 true；否则为 false。</returns>
        public static bool IsFax(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            string faxRegex = @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$";
            return Regex.IsMatch(str, faxRegex);
        }

        /// <summary>
        /// 判断字符串是否为ip
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>是否为ip</returns>
        public static bool IsIP(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 判断字符串是否只包含字母、数字和汉字
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns></returns>
        public static bool IsLetterDigitOrChinese(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            return Regex.IsMatch(str, "^[a-z0-9A-Z\u4e00-\u9fa5]+$");
        }
    }
}
